package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.SensitiveMapper;
import com.heima.admin.service.SensitiveService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * @ClassName SensitiveServiceImpl
 * @Description TODO
 * @Author AWei
 * @DATE 2021/4/10 19:30
 * @Version 1.0
 */
@Service
@Transactional
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, AdSensitive> implements SensitiveService {

    @Override
    public ResponseResult querySensitiveByPageOrName(SensitiveDto sensitiveDto) {
        //1. 检验参数：参数对象是否为空，分页数据是否存在，否则设为默认值
        if (ObjectUtils.isEmpty(sensitiveDto)) {
            CustomException.custom(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        sensitiveDto.checkParam();
        //2. 判断参数对象名称（name）是否为空，若不为空则添加模糊查询条件
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(sensitiveDto.getName())) {
            queryWrapper.like(AdSensitive::getSensitives, sensitiveDto.getName());
        }
        //3. 分页查询并返回结果对象
        Page<AdSensitive> page = new Page<>(sensitiveDto.getPage(), sensitiveDto.getSize());
        IPage<AdSensitive> pageResult = page(page, queryWrapper);
        ResponseResult responseResult = new PageResponseResult(sensitiveDto.getPage(), sensitiveDto.getSize(), pageResult.getTotal());
        responseResult.setData(pageResult.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult addSensitive(AdSensitive adSensitive) {
        //1. 校验参数：敏感词id是否存在
        if (ObjectUtils.isEmpty(adSensitive.getId())) {
            CustomException.custom(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2. 判断数据库中是否存在该关键字
        AdSensitive one = getOne(new LambdaQueryWrapper<AdSensitive>().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (ObjectUtils.isEmpty(one)) {
            CustomException.custom(AppHttpCodeEnum.DATA_EXIST);
        }
        //3. 添加敏感词信息
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult modifySensitive(AdSensitive adSensitive) {
        //1. 检验参数：参数id变量和参数adChannel不能为null
        if (ObjectUtils.isEmpty(adSensitive.getId()) || ObjectUtils.isEmpty(adSensitive)) {
            CustomException.custom(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2. 查询该频道信息是否存在
        AdSensitive channel = getById(adSensitive.getId());
        if (channel == null) {
            CustomException.custom(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //存在，修改频道信息
        updateById(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteSensitive(Integer id) {
        //1. 检验参数：id（频道id）是否为空
        if (ObjectUtils.isEmpty(id)) {
            CustomException.custom(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2. 查询并判断该敏感词是否为空
        AdSensitive adSensitive = getById(id);
        if (ObjectUtils.isEmpty(adSensitive)) {
            CustomException.custom(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3. 删除该敏感词
        removeById(id);
        return ResponseResult.okResult();
    }

}
